import modelExtend from 'dva-model-extend'
import { lwjPageModel } from 'utils/model'
import { routerRedux } from 'dva/router'
import { Modal, message } from 'antd'
import { config } from 'utils'
import { query,queryTypeList,queryAllStoreList,linkedStoreList,materialList,link } from './services/product'

export default modelExtend(lwjPageModel, {
  //命名空间
  namespace: 'productlist',
  //数据
  state: {
    type: "",
    list: [],
    //分页
    pagination: {
      //与lwjTable交互要用current
      current: 1,
      total: 0,
      showTotal: total => `共 ${total} 条`,
      showSizeChanger: true,
      showQuickJumper: true,
      defaultPageSize: 10,
      pageSize: 10
    },
    //筛选参数
    queryParam: {},
    classifyList: [],
    classifySearchKeyword: '',
    classifyId: null,
    storeVisible: false,
    storeList: [],
    targetStoreKeys: [],
    materielVisible: false,
    materialList: [],
    id: null
  },
  subscriptions: {
    //页面初始化事件
    setup({dispatch, history}) {
      history.listen(({ pathname }) => {
        if (pathname === '/product/list') {
          dispatch({
            type: 'reset'
          })
          dispatch({ type: 'query', payload: {
            start: 0,
            limit: 10
          }})
          dispatch({ type: 'queryTypeList'})
          dispatch({ type: 'queryStoreList'})
        }
      })
    }
  },
  effects: {
    //请求异步数据
    * query({payload}, { select, call, put }) {
      const queryParam = yield select(({ productlist }) => productlist.queryParam);
      const classifyId = yield select(({ productlist }) => productlist.classifyId);
      queryParam.cateIdAndChild = classifyId;
      //调用异步service
      const {result : data} = yield call(query, {
        // 与service交互要用current,但是lwjTable返回的是start
        start: payload.start,
        limit: payload.limit,
        sort: 'id',
        order: 'DESC',
        ...queryParam
      });
      //调用同步事件
      yield put({
        type: 'success',
        payload: {
          list: data.content,
          pagination: {
              current: payload.start + 1,
              total: data.totalElements,
              showTotal: total => `共 ${total} 条`,
              showSizeChanger: true,
              showQuickJumper: true,
              defaultPageSize: 10,
              pageSize: data.size
          }
        },
      });
    },
    *queryStoreList({}, {select, call, put}) {
      const {result: data} = yield call(queryAllStoreList);
      yield put({
        type: 'setStoreList',
        storeList: data.distributorSummaries
      })
    },
    //商品类目列表
    *queryTypeList({}, { select, call, put }) {
      const {result: data} = yield call(queryTypeList);
      yield put({
        type: 'setClassifyList',
        classifyList: data
      })
    },
    //门店列表
    *storeLinkedList({id}, { select, call, put }) {
      const {result: data} = yield call(linkedStoreList,{
        id
      });
      let _temp = data.map((value) => value.id)
      yield put({
        type: 'setLinkedStoreList',
        targetStoreKeys: _temp
      })
    },
    //物料列表
    *materialList({id}, { select, call, put }) {
      const {result: data} = yield call(materialList,{
        id
      });
      yield put({
        type: 'setMaterialList',
        materialList: data
      })
    },
    //关联&取关
    *link({param}, { select, call, put }) {
      yield call(link, param)
      yield put({
        type: 'toggleModal',
        name: 'store',
        flag: false
      })
      message.success("操作成功");
    }
  },
  reducers: {
    //同步state数据
    success(state, {payload}) {
      return {
        ...state,
        list: payload.list,
        pagination: payload.pagination
      }
    },
    //同步state数据
    changeFilter(state, {queryParam}) {
      return {
        ...state,
          queryParam
      }
    },
    toggleModal(state, {name, flag}) {
      let _temp = {};
      _temp[name+'Visible'] = flag;
      if(name == 'material' && flag==false){
        _temp.materialList = [];
      }
      if(name == 'store' && flag==false){
        _temp.targetStoreKeys = [];
      }
      return{
        ...state,
        ..._temp
      }
    },
    setClassifyList(state, {classifyList}) {
      return{
        ...state,
        classifyList
      }
    },
    setClassifyId(state, {id}) {
      return{
        ...state,
        classifyId: id
      }
    },
    setClassifySearchKeyword(state, {keyword}) {
      return{
        ...state,
        classifySearchKeyword: keyword
      }
    },
    setMaterialList(state, {materialList}) {
      return{
        ...state,
        materialList
      }
    },
    setStoreList(state, {storeList}) {
      return{
        ...state,
        storeList
      }
    },
    setLinkedStoreList(state, {targetStoreKeys}) {
      return{
        ...state,
        targetStoreKeys
      }
    },
    setSelectedId(state, {id}) {
      return{
        ...state,
        id
      }
    },
    reset(){
      return{
        type: "",
        list: [],
        //分页
        pagination: {
          //与lwjTable交互要用current
          current: 1,
          total: 0,
          showTotal: total => `共 ${total} 条`,
          showSizeChanger: true,
          showQuickJumper: true,
          defaultPageSize: 10,
          pageSize: 10
        },
        //筛选参数
        queryParam: {},
        classifyList: [],
        classifySearchKeyword: '',
        classifyId: null,
        storeVisible: false,
        storeList: [],
        targetStoreKeys: [],
        materielVisible: false,
        materialList: [],
        id: null
      }
    }
  }
})
